/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.UserDetail;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Young Ran
 */
public class UserDetailDAO {
    
    private Connection cn;
    private PreparedStatement pstm;
    private CallableStatement cs;
    
    public UserDetailDAO() {
        cn = DB_Util.getConnection();
    }
    
    private ArrayList<UserDetail> getUserDetailData(ResultSet rs) throws SQLException {
        ArrayList<UserDetail> list = new ArrayList<UserDetail>();
        while (rs.next()) {
            UserDetail e = new UserDetail();
            e.setId(rs.getInt("UserDetailId"));
            e.setName(rs.getString("UserDetailName"));
            e.setAge(rs.getString("UserDetailAge"));
            e.setSex(rs.getBoolean("UserDetailSex"));
            e.setPhone(rs.getString("UserDetailPhone"));
            e.setEmail(rs.getString("UserDetailEmail"));
            e.setUsername(rs.getString("AccountUsername"));                       
            list.add(e);
        }
        return list;
    }
    
    //DISPLAY USER DETAILS:
    public ArrayList<UserDetail> getAllUserDetail() throws SQLException {
        cs = cn.prepareCall("{call SP_Display_UserDetail}");
        ResultSet rs = cs.executeQuery();
        return getUserDetailData(rs);
    }
    
    public ArrayList<UserDetail> getAllUnapprovedUserDetail() throws SQLException {
        cs = cn.prepareCall("{call SP_Display_UnapprovedUserDetail}");
        ResultSet rs = cs.executeQuery();
        return getUserDetailData(rs);
    }
    
    public ArrayList<UserDetail> getByUName(String username) throws SQLException {
        cs = cn.prepareCall("{call SP_Search_UserDetailByUName(?)}");
        cs.setString(1, username);
        ResultSet rs = cs.executeQuery();
        return getUserDetailData(rs);
    }
    
    public void addUserDetail(UserDetail item) throws SQLException
    {
        cs = cn.prepareCall("{call SP_Add_UserDetail(?,?,?,?,?,?)}");              
        cs.setNString(1, item.getName());
        cs.setInt(2, Integer.parseInt(item.getAge()));
        cs.setBoolean(3, item.isSex());
        cs.setString(4, item.getPhone());
        cs.setString(5, item.getEmail());
        cs.setString(6, item.getUsername());
        cs.execute();
    }
    
    public void approveUser(int userId) throws SQLException
    {
        cs = cn.prepareCall("{call SP_Approve_UserDetail(?)}");
        cs.setInt(1, userId);
        cs.execute();
    }
    
    public void editUserDetail(UserDetail item) throws SQLException{
        cs = cn.prepareCall("{call SP_Edit_UserDetail(?,?,?,?,?,?)}");
        cs.setInt(1,item.getId());
        cs.setNString(2, item.getName());
        cs.setString(3, item.getAge());
        cs.setBoolean(4, item.isSex());
        cs.setString(5, item.getPhone());
        cs.setString(6, item.getEmail());
        cs.execute();
    }
}
